/*
 * @Author: 晴鸢. 3013433857@qq.com
 * @Date: 2025-09-23 11:52:47
 * @LastEditors: 晴鸢. 3013433857@qq.com
 * @LastEditTime: 2025-09-24 17:16:10
 * @FilePath: /Learn/blockqueue/main.cc
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
#include"blockqueue.hpp"
#include"Task.hpp"
#include<ctime>
void*Consumer(void*args)
{
    BlockQueue<Task>*bq=static_cast<BlockQueue<Task>*>(args);
    while(1)
    {
        //消费
        Task t=bq->pop();
        t.run();
        cout<<"处理了一个任务："<<t.getTask()<<",结果："
        <<t.Getresult()<<" thread id:"<<pthread_self()<<endl;
        //计算和处理数据..
    }
}

void*Productor(void*args)
{
    int len=opers.size();
    BlockQueue<Task>*bq=static_cast<BlockQueue<Task>*>(args);
    while(1)
    {
        //模拟生产者生产数据
        int data1=rand()%10+1;
        usleep(10);
        int data2=rand()%10+1;
        usleep(10);
        char op=opers[rand()%len];
        Task t(data1,data2,op);
        //生产
        bq->push(t);
        cout<<"生产了一个数据:"<<t.getTask()<<
        " thread id:"<<pthread_self()<<endl;
        sleep(1);
    }
}
int main()
{
    srand(time(nullptr));
    pthread_t c[3],p[5];
    BlockQueue<Task> *bq=new BlockQueue<Task>();
    for(int i=0;i<3;i++)
    {
        pthread_create(c+i,nullptr,Consumer,bq);
    }
    for(int i=0;i<5;i++)
    {
        pthread_create(p+i,nullptr,Productor,bq);
    }

   for(int i=0;i<3;i++) pthread_join(c[i],nullptr);
   for(int i=0;i<5;i++) pthread_join(p[i],nullptr);
    delete bq;
    return 0;
}